/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.datatables.CharacterTable;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.ServerBasePacket;

public class L1Clan {

	/**[見習]*/
	public static final int CLAN_RANK_PROBATION = 1;

	/**[一般] 無倉庫使用權*/
	public static final int CLAN_RANK_PUBLIC = 2;

	/**[副君主]*/
	public static final int CLAN_RANK_GUARDIAN = 3;

	/**[聯盟君主]*/
	public static final int CLAN_RANK_PRINCE = 4;

	/**聯合血盟[修習騎士]*/
	public static final int ALLIANCE_CLAN_RANK_ATTEND = 5;
	
	/**聯合血盟[守護騎士]*/
	public static final int ALLIANCE_CLAN_RANK_GUARDIAN = 6;
	
	/**一般血盟[一般]*/
	public static final int NORMAL_CLAN_RANK_GENERAL = 7;
	
	/**一般血盟[修習騎士]*/
	public static final int NORMAL_CLAN_RANK_ATTEND = 8;
	
	/**一般血盟[守護騎士]*/
	public static final int NORMAL_CLAN_RANK_GUARDIAN = 9;
	
	/**一般血盟[聯盟君主]*/
	public static final int NORMAL_CLAN_RANK_PRINCE = 10;

	private static final Logger _log = Logger.getLogger(L1Clan.class.getName());

	private int _clanId;

	private String _clanName;

	private int _leaderId;

	private String _leaderName;

	private int _castleId;

	private int _houseId;

	private int _warehouse = 0;

	private final L1DwarfForClanInventory _dwarfForClan = new L1DwarfForClanInventory(this);

	private final ArrayList<String> membersNameList = new ArrayList<String>();

	public void addMemberName(final String member_name) {
		if (!this.membersNameList.contains(member_name)) {
			this.membersNameList.add(member_name);
		}
	}

	public void delMemberName(final String member_name) {
		if (this.membersNameList.contains(member_name)) {
			this.membersNameList.remove(member_name);
		}
	}

	public String[] getAllMembers() {
		return this.membersNameList.toArray(new String[this.membersNameList.size()]);
	}

	public String getAllMembersFP() {
		String result = "";
		for (final String name : this.membersNameList) {
			result = result + name + " ";
		}
		return result;
	}

	public String getAllMembersFPWithRank() {
		String result = "";
		try {
			for (final String name : this.membersNameList) {
				final L1PcInstance pc = CharacterTable.getInstance()
				.restoreCharacter(name);
				if (pc != null) {
					result = result + name + this.getRankString(pc) + " ";
				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		return result;
	}

	public int getCastleId() {
		return this._castleId;
	}

	public int getClanId() {
		return this._clanId;
	}

	public String getClanName() {
		return this._clanName;
	}

	public L1DwarfForClanInventory getDwarfForClanInventory() {
		return this._dwarfForClan;
	}

	public int getHouseId() {
		return this._houseId;
	}

	public int getLeaderId() {
		return this._leaderId;
	}

	public String getLeaderName() {
		return this._leaderName;
	}

	public L1PcInstance[] getOnlineClanMember() // オンライン中のクラン員のみ
	{
		final ArrayList<L1PcInstance> onlineMembers = new ArrayList<L1PcInstance>();
		for (final String name : this.membersNameList) {
			final L1PcInstance pc = L1World.getInstance().getPlayer(name);
			if (pc != null && !onlineMembers.contains(pc)) {
				onlineMembers.add(pc);
			}
		}
		return onlineMembers.toArray(new L1PcInstance[onlineMembers.size()]);
	}

	public String getOnlineMembersFP() { // FP means "For Pledge"
		String result = "";
		for (final String name : this.membersNameList) {
			final L1PcInstance pc = L1World.getInstance().getPlayer(name);
			if (pc != null) {
				result = result + name + " ";
			}
		}
		return result;
	}

	public String getOnlineMembersFPWithRank() {
		String result = "";
		for (final String name : this.membersNameList) {
			final L1PcInstance pc = L1World.getInstance().getPlayer(name);
			if (pc != null) {
				result = result + name + this.getRankString(pc) + " ";
			}
		}
		return result;
	}
	private static String[] _rank = new String[]{
			// 2:一般 3:副君主 4:聯盟君主 5:修習騎士 6:守護騎士 7:一般 8:修習騎士 9:守護騎士 10:聯盟君主
			"","","[一般]","[副君主]","[聯盟君主]","[修習騎士]","[守護騎士]","[一般]","[修習騎士]","[守護騎士]","[聯盟君主]",
	};
	/**
	 * 血盟階級
	 * @param pc
	 * @return
	 */
	public String getRankString(final L1PcInstance pc) {
		if (pc != null) {
			if (pc.getClanRank() > 0) {
				return _rank[pc.getClanRank()];
			}
		}
		return "";
	}
//	private String getRankString(final L1PcInstance pc) {
//		String rank = "";
//		if (pc != null) {
//			if (pc.getClanRank() == CLAN_RANK_PROBATION) {
//				rank = "[見習]";
//			} else if (pc.getClanRank() == CLAN_RANK_PUBLIC) {
//				rank = "[一般]";
//			} else if (pc.getClanRank() == CLAN_RANK_GUARDIAN) {
//				rank = "[守護騎士]";
//			} else if (pc.getClanRank() == CLAN_RANK_PRINCE) {
//				rank = "[血盟王族]";
//			} else {
//				rank = "";
//			}
//		}
//		return rank;
//	}

	public int getWarehouseUsingChar() {
		return this._warehouse;
	}

	/**
	 * 對血盟線上成員發送封包
	 */
	public void sendPacketsAll(final ServerBasePacket packet) {
		try {
			for (final Object nameobj : this.membersNameList.toArray()) {
				final String name = (String) nameobj;
				final L1PcInstance pc = L1World.getInstance().getPlayer(name);
				if (pc != null) {
					pc.sendPackets(packet);
				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		}
	}

	public void setCastleId(final int hasCastle) {
		this._castleId = hasCastle;
	}

	public void setClanId(final int clan_id) {
		this._clanId = clan_id;
	}

	public void setClanName(final String clan_name) {
		this._clanName = clan_name;
	}

	public void setHouseId(final int hasHideout) {
		this._houseId = hasHideout;
	}

	public void setLeaderId(final int leader_id) {
		this._leaderId = leader_id;
	}

	public void setLeaderName(final String leader_name) {
		this._leaderName = leader_name;
	}
	public void setWarehouseUsingChar(final int objid) {
		this._warehouse = objid;
	}
}
